<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <div><h1 id="node-js-http-">Node.js 的 http 模块</h1>
<h2 id="labdoc-header-0">一、实验介绍</h2>
<h3 id="labdoc-header-1">1.1 实验内容</h3>
<p>Web 服务器最重要的功能是什么？当然是处理<code>http</code>请求啦。本次实验就带大家探索<code>http</code>模块。<br>此次实验还会讲到url模块和path模块，同时也会用到前面讲过的fs模块。url模块用于解析url，path模块用于处理和转换文件路径。</p><h3 id="labdoc-header-2">1.2 实验知识点</h3>
<ul>
<li><code>http</code>模块的使用</li>
<li><code>url</code>模块</li>
<li><code>path</code>模块</li>
</ul>
<h3 id="labdoc-header-3">1.3 实验环境</h3>
<ul>
<li>Xfce终端</li>
<li>Node.js v4.2.1</li>
<li>gedit/vim：文本编辑</li>
</ul>
<h2 id="labdoc-header-4">二、实验步骤</h2>
<h3 id="labdoc-header-5">2.1 创建http server</h3>
<p>通过Node.js创建http server非常简单，示例代码如下：</p><pre><code class="hljs javascript"><span class="hljs-comment"><span class="hljs-comment">// 文件名：demo.js</span></span>

<span class="hljs-comment"><span class="hljs-comment">// 引入http模块</span></span>
<span class="hljs-keyword"><span class="hljs-keyword">var</span></span> http = <span class="hljs-built_in"><span class="hljs-built_in">require</span></span>(<span class="hljs-string"><span class="hljs-string">'http'</span></span>);

<span class="hljs-comment"><span class="hljs-comment">// 创建http server</span></span>
http.createServer(<span class="hljs-function"><span class="hljs-keyword"><span class="hljs-function"><span class="hljs-keyword">function</span></span></span><span class="hljs-function"> (</span><span class="hljs-params"><span class="hljs-function"><span class="hljs-params">req, res</span></span></span><span class="hljs-function">) </span></span>{
    res.writeHead(<span class="hljs-number"><span class="hljs-number">200</span></span>, {<span class="hljs-string"><span class="hljs-string">'Content-Type'</span></span>: <span class="hljs-string"><span class="hljs-string">'text/plain'</span></span>});
    res.end(<span class="hljs-string"><span class="hljs-string">'Hello World\n'</span></span>);
}).listen(<span class="hljs-number"><span class="hljs-number">1337</span></span>, <span class="hljs-string"><span class="hljs-string">'127.0.0.1'</span></span>);

<span class="hljs-built_in"><span class="hljs-built_in">console</span></span>.log(<span class="hljs-string"><span class="hljs-string">'Server running at http://127.0.0.1:1337/'</span></span>);
</code></pre><p>运行此文件：</p><pre><code class="hljs ruby">$ <span class="hljs-keyword">node</span> <span class="hljs-title">demo</span>.js
</code></pre><p>然后打开虚拟机浏览器，访问“<a href="http://127.0.0.1:1337/”，就会看到页面上显示了“Hello" target="_blank">http://127.0.0.1:1337/”，就会看到页面上显示了“Hello</a> World”，说明我们的http server创建成功了。</p><p>当然，我们在这个实验要做的比这个稍微复杂一点。</p><p>在这个实验中，我们会创建一个简单的http server，所有的代码都放在app这个文件夹中。文件夹结构如下：</p><pre><code class="hljs css"><span class="hljs-selector-tag">app</span>
├── <span class="hljs-selector-tag">main</span><span class="hljs-selector-class"><span class="hljs-selector-class">.js</span></span>
├── <span class="hljs-selector-tag">requestHandlers</span><span class="hljs-selector-class"><span class="hljs-selector-class">.js</span></span>
├── <span class="hljs-selector-tag">router</span><span class="hljs-selector-class"><span class="hljs-selector-class">.js</span></span>
├── <span class="hljs-selector-tag">server</span><span class="hljs-selector-class"><span class="hljs-selector-class">.js</span></span>
└── <span class="hljs-selector-tag">views</span>
    ├── <span class="hljs-number">404</span><span class="hljs-selector-class"><span class="hljs-selector-class">.html</span></span>
    ├── <span class="hljs-selector-tag">about</span><span class="hljs-selector-class"><span class="hljs-selector-class">.html</span></span>
    └── <span class="hljs-selector-tag">home</span><span class="hljs-selector-class"><span class="hljs-selector-class">.html</span></span>

</code></pre><p>首先，新建一个app文件夹，在文件夹中新建<code>server.js</code>文件，输入如下代码（其中的注释为代码解释）：</p><pre><code class="hljs javascript"><span class="hljs-comment"><span class="hljs-comment">//</span></span>
<span class="hljs-comment"><span class="hljs-comment">// 创建http server</span></span>
<span class="hljs-comment"><span class="hljs-comment">//</span></span>

<span class="hljs-comment"><span class="hljs-comment">// 加载所需模块</span></span>
<span class="hljs-keyword"><span class="hljs-keyword">var</span></span> http = <span class="hljs-built_in"><span class="hljs-built_in">require</span></span>(<span class="hljs-string"><span class="hljs-string">'http'</span></span>);
<span class="hljs-keyword"><span class="hljs-keyword">var</span></span> <span class="hljs-built_in">url</span> = <span class="hljs-built_in"><span class="hljs-built_in">require</span></span>(<span class="hljs-string"><span class="hljs-string">'url'</span></span>);
<span class="hljs-keyword"><span class="hljs-keyword">var</span></span> fs = <span class="hljs-built_in"><span class="hljs-built_in">require</span></span>(<span class="hljs-string"><span class="hljs-string">'fs'</span></span>);

<span class="hljs-comment"><span class="hljs-comment">// 设置ip和端口</span></span>
<span class="hljs-comment"><span class="hljs-comment">// 实际应用中，可以把这些写到配置文件中</span></span>
<span class="hljs-keyword"><span class="hljs-keyword">var</span></span> host = <span class="hljs-string"><span class="hljs-string">'127.0.0.1'</span></span>,
    port = <span class="hljs-number"><span class="hljs-number">8080</span></span>;

<span class="hljs-comment"><span class="hljs-comment">// 创建http server</span></span>
<span class="hljs-function"><span class="hljs-keyword"><span class="hljs-function"><span class="hljs-keyword">function</span></span></span><span class="hljs-function"> </span><span class="hljs-title"><span class="hljs-function"><span class="hljs-title">start</span></span></span><span class="hljs-function">(</span><span class="hljs-params"><span class="hljs-function"><span class="hljs-params">route, handle</span></span></span><span class="hljs-function">) </span></span>{
    <span class="hljs-comment"><span class="hljs-comment">// 参数</span></span>
    <span class="hljs-comment"><span class="hljs-comment">// route  判断url是否存在，存在则调用handle处理，不存在则返回404</span></span>
    <span class="hljs-comment"><span class="hljs-comment">// handle 处理不同的url请求</span></span>


    <span class="hljs-comment"><span class="hljs-comment">// 处理request请求</span></span>
    <span class="hljs-function"><span class="hljs-keyword"><span class="hljs-function"><span class="hljs-keyword">function</span></span></span><span class="hljs-function"> </span><span class="hljs-title"><span class="hljs-function"><span class="hljs-title">onRequest</span></span></span><span class="hljs-function">(</span><span class="hljs-params"><span class="hljs-function"><span class="hljs-params">req, res</span></span></span><span class="hljs-function">) </span></span>{
        <span class="hljs-comment"><span class="hljs-comment">// 使用url.parse()方法解析url</span></span>
        <span class="hljs-comment"><span class="hljs-comment">// 它会把url string转化为一个object</span></span>
        <span class="hljs-comment"><span class="hljs-comment">// 这样我们就可以很方便的获取url中的host、port、pathname等值了</span></span>
        <span class="hljs-keyword"><span class="hljs-keyword">var</span></span> pathname = <span class="hljs-built_in">url</span>.parse(req.url).pathname;
        <span class="hljs-built_in"><span class="hljs-built_in">console</span></span>.log(<span class="hljs-string"><span class="hljs-string">'Request for '</span></span> + pathname + <span class="hljs-string"><span class="hljs-string">' received.'</span></span>);

        <span class="hljs-comment"><span class="hljs-comment">// 判断并处理不同url请求</span></span>
        <span class="hljs-comment"><span class="hljs-comment">// 后面介绍此方法</span></span>
        route(handle, pathname, res, req);
    }

    <span class="hljs-comment"><span class="hljs-comment">// 使用http.createSserver()方法创建http server</span></span>
    <span class="hljs-comment"><span class="hljs-comment">// 并传入onRequest()方法</span></span>
    <span class="hljs-comment"><span class="hljs-comment">// 然后使用listen()方法监听指定地址</span></span>
    http.createServer(onRequest).listen(port, host);
    <span class="hljs-built_in"><span class="hljs-built_in">console</span></span>.log(<span class="hljs-string"><span class="hljs-string">'Server has started and listening on '</span></span> + host + <span class="hljs-string"><span class="hljs-string">':'</span></span> + port);
}

<span class="hljs-comment"><span class="hljs-comment">// 导出 start 方法</span></span>
exports.start = start;

</code></pre><p>在文件的最后，我们导出了start方法，以便在主程序中使用。你肯定注意到了，在代码中使用了<code>route()</code>方法，它用于处理判断请求的url是否存在，现在我们就来编写这个方法。</p><h3 id="labdoc-header-6">2.2 创建路由</h3>
<p>在app文件夹中新建<code>router.js</code>，输入如下代码：</p><pre><code class="hljs javascript"><span class="hljs-keyword"><span class="hljs-keyword">var</span></span> fs = <span class="hljs-built_in"><span class="hljs-built_in">require</span></span>(<span class="hljs-string"><span class="hljs-string">'fs'</span></span>);

<span class="hljs-comment"><span class="hljs-comment">// 路由函数</span></span>
<span class="hljs-comment"><span class="hljs-comment">// 处理不同url的请求</span></span>
<span class="hljs-comment"><span class="hljs-comment">// 并返回相应内容</span></span>

<span class="hljs-function"><span class="hljs-keyword"><span class="hljs-function"><span class="hljs-keyword">function</span></span></span><span class="hljs-function"> </span><span class="hljs-title"><span class="hljs-function"><span class="hljs-title">route</span></span></span><span class="hljs-function">(</span><span class="hljs-params"><span class="hljs-function"><span class="hljs-params">handle, pathname, res, req</span></span></span><span class="hljs-function">) </span></span>{
    <span class="hljs-built_in"><span class="hljs-built_in">console</span></span>.log(<span class="hljs-string"><span class="hljs-string">'About to route a request for '</span></span> + pathname);

    <span class="hljs-comment"><span class="hljs-comment">// 判断此url是否存在特定处理函数</span></span>
    <span class="hljs-comment"><span class="hljs-comment">// 存在则调用handle处理</span></span>
    <span class="hljs-comment"><span class="hljs-comment">// 不存在则返回404页面</span></span>
    <span class="hljs-keyword"><span class="hljs-keyword">if</span></span> (<span class="hljs-keyword"><span class="hljs-keyword">typeof</span></span> handle[pathname] === <span class="hljs-string"><span class="hljs-string">'function'</span></span>) {
        <span class="hljs-comment"><span class="hljs-comment">// 后面介绍handle函数</span></span>
        handle[pathname](res, req);
    } <span class="hljs-keyword"><span class="hljs-keyword">else</span></span> {
        <span class="hljs-built_in"><span class="hljs-built_in">console</span></span>.log(<span class="hljs-string"><span class="hljs-string">'No request handler found for '</span></span> + pathname);

        <span class="hljs-comment"><span class="hljs-comment">// 读取404页面</span></span>
        <span class="hljs-comment"><span class="hljs-comment">// 所有页面都存放在view文件夹下</span></span>
        <span class="hljs-keyword"><span class="hljs-keyword">var</span></span> content = fs.readFileSync(<span class="hljs-string"><span class="hljs-string">'./views/404.html'</span></span>);
        res.writeHead(<span class="hljs-number"><span class="hljs-number">404</span></span>, { <span class="hljs-string"><span class="hljs-string">'Content-Type'</span></span>: <span class="hljs-string"><span class="hljs-string">'text/html'</span></span> });
        res.write(content);
        res.end();
    }
}
<span class="hljs-comment"><span class="hljs-comment">// 导出 route 方法</span></span>
exports.route = route;

</code></pre><p>在此方法中，调用了<code>handle()</code>方法，这个方法用于处理不同的url请求。</p><p>在app文件夹中新建<code>requestHandlers.js</code>文件，输入如下代码：</p><pre><code class="hljs javascript"><span class="hljs-comment"><span class="hljs-comment">// 处理url请求</span></span>

<span class="hljs-keyword"><span class="hljs-keyword">var</span></span> fs = <span class="hljs-built_in"><span class="hljs-built_in">require</span></span>(<span class="hljs-string"><span class="hljs-string">'fs'</span></span>);

<span class="hljs-comment"><span class="hljs-comment">// home.html 主页</span></span>
<span class="hljs-function"><span class="hljs-keyword"><span class="hljs-function"><span class="hljs-keyword">function</span></span></span><span class="hljs-function"> </span><span class="hljs-title"><span class="hljs-function"><span class="hljs-title">home</span></span></span><span class="hljs-function">(</span><span class="hljs-params"><span class="hljs-function"><span class="hljs-params">res</span></span></span><span class="hljs-function">) </span></span>{
    <span class="hljs-built_in"><span class="hljs-built_in">console</span></span>.log(<span class="hljs-string"><span class="hljs-string">'Request handler "home" was called.'</span></span>);

    <span class="hljs-comment"><span class="hljs-comment">// 读取home.html文件</span></span>
    <span class="hljs-keyword"><span class="hljs-keyword">var</span></span> content = fs.readFileSync(<span class="hljs-string"><span class="hljs-string">'./views/home.html'</span></span>);
    res.writeHead(<span class="hljs-number"><span class="hljs-number">200</span></span>, { <span class="hljs-string"><span class="hljs-string">'Content-Type'</span></span>: <span class="hljs-string"><span class="hljs-string">'text/html'</span></span> });
    res.write(content);
    res.end();
}

<span class="hljs-comment"><span class="hljs-comment">// about.html 关于页面</span></span>
<span class="hljs-function"><span class="hljs-keyword"><span class="hljs-function"><span class="hljs-keyword">function</span></span></span><span class="hljs-function"> </span><span class="hljs-title"><span class="hljs-function"><span class="hljs-title">about</span></span></span><span class="hljs-function">(</span><span class="hljs-params"><span class="hljs-function"><span class="hljs-params">res</span></span></span><span class="hljs-function">) </span></span>{
    <span class="hljs-built_in"><span class="hljs-built_in">console</span></span>.log(<span class="hljs-string"><span class="hljs-string">'Request handler "about" was called.'</span></span>);

    <span class="hljs-comment"><span class="hljs-comment">// 读取about.html文件</span></span>
    <span class="hljs-keyword"><span class="hljs-keyword">var</span></span> content = fs.readFileSync(<span class="hljs-string"><span class="hljs-string">'./views/about.html'</span></span>);
    res.writeHead(<span class="hljs-number"><span class="hljs-number">200</span></span>, { <span class="hljs-string"><span class="hljs-string">'Content-Type'</span></span>: <span class="hljs-string"><span class="hljs-string">'text/html'</span></span> });
    res.write(content);
    res.end();
}

<span class="hljs-comment"><span class="hljs-comment">// 导出页面处理函数</span></span>
exports.home = home;
exports.about = about;

</code></pre><p>这个方法比较简单，就是读取文件，然后输出到response。</p><h3 id="labdoc-header-7">2.3 创建主程序</h3>
<p>创建http server，判断url，处理url都写完了，那么我们可以写主程序来运行http server了，在app文件夹新建<code>main.js</code>文件，输入如下代码：</p><pre><code class="hljs javascript"><span class="hljs-comment"><span class="hljs-comment">// 主程序</span></span>

<span class="hljs-comment"><span class="hljs-comment">// 引入server，router及requestHandler</span></span>
<span class="hljs-built_in"><span class="hljs-keyword">var</span></span> server = <span class="hljs-keyword"><span class="hljs-built_in">require</span></span>(<span class="hljs-string"><span class="hljs-string">'./server'</span></span>);
<span class="hljs-built_in"><span class="hljs-keyword">var</span></span> router = <span class="hljs-keyword"><span class="hljs-built_in">require</span></span>(<span class="hljs-string"><span class="hljs-string">'./router'</span></span>);
<span class="hljs-built_in"><span class="hljs-keyword">var</span></span> requestHandlers = <span class="hljs-keyword"><span class="hljs-built_in">require</span></span>(<span class="hljs-string"><span class="hljs-string">'./requestHandlers'</span></span>);

<span class="hljs-comment"><span class="hljs-comment">// 保存url处理方法</span></span>
<span class="hljs-built_in"><span class="hljs-keyword">var</span></span> <span class="hljs-keyword">handle</span> = {};
<span class="hljs-keyword">handle</span><span class="hljs-meta">[</span><span class="hljs-string"><span class="hljs-string">'/'</span></span><span class="hljs-meta">]</span> = requestHandlers.home;
handle<span class="hljs-meta">[</span><span class="hljs-string"><span class="hljs-string">'/about'</span></span><span class="hljs-meta">]</span> = requestHandlers.about;

<span class="hljs-comment">// 启动http server</span>
server.start(router.route, handle);

</code></pre><p>到此，所有的服务器代码都写完了，那么我们来添加代码中用到的两个html文件吧。</p><h3 id="labdoc-header-8">2.4 创建HTML文件</h3>
<p>在app文件夹中新建views文件夹，在views文件夹中，新建<code>home.html</code>文件、<code>about.html</code>文件和<code>404.html</code>文件。</p><p>文件中的代码如下所示：</p><p><code>home.html</code>文件：</p><pre><code class="hljs xml"><span class="hljs-meta"><span class="hljs-meta">&lt;!DOCTYPE html&gt;</span></span>
<span class="hljs-tag"><span class="hljs-tag">&lt;</span><span class="hljs-name"><span class="hljs-tag"><span class="hljs-name">html</span></span></span><span class="hljs-tag">&gt;</span></span>
    <span class="hljs-tag"><span class="hljs-tag">&lt;</span><span class="hljs-name"><span class="hljs-tag"><span class="hljs-name">head</span></span></span><span class="hljs-tag">&gt;</span></span>
        <span class="hljs-tag"><span class="hljs-tag">&lt;</span><span class="hljs-name"><span class="hljs-tag"><span class="hljs-name">meta</span></span></span><span class="hljs-tag"> </span><span class="hljs-attr"><span class="hljs-tag"><span class="hljs-attr">charset</span></span></span><span class="hljs-tag">=</span><span class="hljs-string"><span class="hljs-tag"><span class="hljs-string">"utf-8"</span></span></span><span class="hljs-tag">&gt;</span></span>
        <span class="hljs-tag"><span class="hljs-tag">&lt;</span><span class="hljs-name"><span class="hljs-tag"><span class="hljs-name">title</span></span></span><span class="hljs-tag">&gt;</span></span>Home page<span class="hljs-tag"><span class="hljs-tag">&lt;/</span><span class="hljs-name"><span class="hljs-tag"><span class="hljs-name">title</span></span></span><span class="hljs-tag">&gt;</span></span>
    <span class="hljs-tag"><span class="hljs-tag">&lt;/</span><span class="hljs-name"><span class="hljs-tag"><span class="hljs-name">head</span></span></span><span class="hljs-tag">&gt;</span></span>
    <span class="hljs-tag"><span class="hljs-tag">&lt;</span><span class="hljs-name"><span class="hljs-tag"><span class="hljs-name">body</span></span></span><span class="hljs-tag">&gt;</span></span>
        <span class="hljs-tag"><span class="hljs-tag">&lt;</span><span class="hljs-name"><span class="hljs-tag"><span class="hljs-name">p</span></span></span><span class="hljs-tag">&gt;</span></span>home page<span class="hljs-tag"><span class="hljs-tag">&lt;/</span><span class="hljs-name"><span class="hljs-tag"><span class="hljs-name">p</span></span></span><span class="hljs-tag">&gt;</span></span>
    <span class="hljs-tag"><span class="hljs-tag">&lt;/</span><span class="hljs-name"><span class="hljs-tag"><span class="hljs-name">body</span></span></span><span class="hljs-tag">&gt;</span></span>
<span class="hljs-tag"><span class="hljs-tag">&lt;/</span><span class="hljs-name"><span class="hljs-tag"><span class="hljs-name">html</span></span></span><span class="hljs-tag">&gt;</span></span>
</code></pre><p><code>about.html</code>文件：</p><pre><code class="hljs xml"><span class="hljs-meta"><span class="hljs-meta">&lt;!DOCTYPE html&gt;</span></span>
<span class="hljs-tag"><span class="hljs-tag">&lt;</span><span class="hljs-name"><span class="hljs-tag"><span class="hljs-name">html</span></span></span><span class="hljs-tag">&gt;</span></span>
    <span class="hljs-tag"><span class="hljs-tag">&lt;</span><span class="hljs-name"><span class="hljs-tag"><span class="hljs-name">head</span></span></span><span class="hljs-tag">&gt;</span></span>
        <span class="hljs-tag"><span class="hljs-tag">&lt;</span><span class="hljs-name"><span class="hljs-tag"><span class="hljs-name">meta</span></span></span><span class="hljs-tag"> </span><span class="hljs-attr"><span class="hljs-tag"><span class="hljs-attr">charset</span></span></span><span class="hljs-tag">=</span><span class="hljs-string"><span class="hljs-tag"><span class="hljs-string">"utf-8"</span></span></span><span class="hljs-tag">&gt;</span></span>
        <span class="hljs-tag"><span class="hljs-tag">&lt;</span><span class="hljs-name"><span class="hljs-tag"><span class="hljs-name">title</span></span></span><span class="hljs-tag">&gt;</span></span>About page<span class="hljs-tag"><span class="hljs-tag">&lt;/</span><span class="hljs-name"><span class="hljs-tag"><span class="hljs-name">title</span></span></span><span class="hljs-tag">&gt;</span></span>
    <span class="hljs-tag"><span class="hljs-tag">&lt;/</span><span class="hljs-name"><span class="hljs-tag"><span class="hljs-name">head</span></span></span><span class="hljs-tag">&gt;</span></span>
    <span class="hljs-tag"><span class="hljs-tag">&lt;</span><span class="hljs-name"><span class="hljs-tag"><span class="hljs-name">body</span></span></span><span class="hljs-tag">&gt;</span></span>
        <span class="hljs-tag"><span class="hljs-tag">&lt;</span><span class="hljs-name"><span class="hljs-tag"><span class="hljs-name">p</span></span></span><span class="hljs-tag">&gt;</span></span>about page<span class="hljs-tag"><span class="hljs-tag">&lt;/</span><span class="hljs-name"><span class="hljs-tag"><span class="hljs-name">p</span></span></span><span class="hljs-tag">&gt;</span></span>
    <span class="hljs-tag"><span class="hljs-tag">&lt;/</span><span class="hljs-name"><span class="hljs-tag"><span class="hljs-name">body</span></span></span><span class="hljs-tag">&gt;</span></span>
<span class="hljs-tag"><span class="hljs-tag">&lt;/</span><span class="hljs-name"><span class="hljs-tag"><span class="hljs-name">html</span></span></span><span class="hljs-tag">&gt;</span></span>
</code></pre><p><code>404.html</code>文件：</p><pre><code class="hljs xml"><span class="hljs-meta"><span class="hljs-meta">&lt;!DOCTYPE html&gt;</span></span>
<span class="hljs-tag"><span class="hljs-tag">&lt;</span><span class="hljs-name"><span class="hljs-tag"><span class="hljs-name">html</span></span></span><span class="hljs-tag">&gt;</span></span>
    <span class="hljs-tag"><span class="hljs-tag">&lt;</span><span class="hljs-name"><span class="hljs-tag"><span class="hljs-name">head</span></span></span><span class="hljs-tag">&gt;</span></span>
        <span class="hljs-tag"><span class="hljs-tag">&lt;</span><span class="hljs-name"><span class="hljs-tag"><span class="hljs-name">meta</span></span></span><span class="hljs-tag"> </span><span class="hljs-attr"><span class="hljs-tag"><span class="hljs-attr">charset</span></span></span><span class="hljs-tag">=</span><span class="hljs-string"><span class="hljs-tag"><span class="hljs-string">"utf-8"</span></span></span><span class="hljs-tag">&gt;</span></span>
        <span class="hljs-tag"><span class="hljs-tag">&lt;</span><span class="hljs-name"><span class="hljs-tag"><span class="hljs-name">title</span></span></span><span class="hljs-tag">&gt;</span></span>404 page<span class="hljs-tag"><span class="hljs-tag">&lt;/</span><span class="hljs-name"><span class="hljs-tag"><span class="hljs-name">title</span></span></span><span class="hljs-tag">&gt;</span></span>
    <span class="hljs-tag"><span class="hljs-tag">&lt;/</span><span class="hljs-name"><span class="hljs-tag"><span class="hljs-name">head</span></span></span><span class="hljs-tag">&gt;</span></span>
    <span class="hljs-tag"><span class="hljs-tag">&lt;</span><span class="hljs-name"><span class="hljs-tag"><span class="hljs-name">body</span></span></span><span class="hljs-tag">&gt;</span></span>
        <span class="hljs-tag"><span class="hljs-tag">&lt;</span><span class="hljs-name"><span class="hljs-tag"><span class="hljs-name">p</span></span></span><span class="hljs-tag">&gt;</span></span>404 page not found<span class="hljs-tag"><span class="hljs-tag">&lt;/</span><span class="hljs-name"><span class="hljs-tag"><span class="hljs-name">p</span></span></span><span class="hljs-tag">&gt;</span></span>
    <span class="hljs-tag"><span class="hljs-tag">&lt;/</span><span class="hljs-name"><span class="hljs-tag"><span class="hljs-name">body</span></span></span><span class="hljs-tag">&gt;</span></span>
<span class="hljs-tag"><span class="hljs-tag">&lt;/</span><span class="hljs-name"><span class="hljs-tag"><span class="hljs-name">html</span></span></span><span class="hljs-tag">&gt;</span></span>
</code></pre><p>HTML文件的代码写得比较简单，可自由发挥。</p><p>那么现在我们来运行程序吧：</p><pre><code class="hljs ruby">$ <span class="hljs-keyword">node</span> <span class="hljs-title">main</span>.js
</code></pre><p>运行成功后，打开虚拟机桌面的浏览器，访问“<a href="http://127.0.0.1:8080”就会看到页面显示“home" target="_blank">http://127.0.0.1:8080”就会看到页面显示“home</a> page”，访问“<a href="http://127.0.0.1:8080/about”就会看到页面显示“about" target="_blank">http://127.0.0.1:8080/about”就会看到页面显示“about</a> page”，访问“<a href="http://127.0.0.1:8080”下的其他页面就会看到页面显示“404" target="_blank">http://127.0.0.1:8080”下的其他页面就会看到页面显示“404</a> page not found”。</p><h2 id="labdoc-header-9">三、实验总结</h2>
<ol>
<li>Node.js的<code>http</code>模块十分强大，只需几行代码就可以启动一个web服务器。</li>
<li><code>http</code>模块中的<code>createServer</code>函数用于创建服务器，对创建出的服务器调用<code>listen</code>函数即可监听http请求。</li>
</ol>
<h2 id="labdoc-header-10">四、课后习题</h2>
<p>请你运用<code>http</code>模块建立一个建议的个人网站，在网站主页中显示你的个人信息。</p></div>
</body>
</html>